package com.h819.examples.j2se;

import org.apache.commons.collections.map.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import static java.lang.System.out;

/**
 * @Title: Collections.java
 * @Description: TODO(添加描述)
 * @author H819
 * @date 2010-3-17
 * @version V1.0
 */
public class CollectionsExamples {

	//private final Collection ArrayList = null;

	/**
	 * commons collectios 的实例代码
	 */
	private void mapExamples() {

		// 多值 map,一个 key 对于多个值
		MultiValueMap mvm = new MultiValueMap();
		mvm.put("1", "one");
		mvm.put("2", "two");
		mvm.put("1", "three");

		for (Iterator it = mvm.entrySet().iterator(); it.hasNext();) {
			Map.Entry entry = (Map.Entry) it.next();
			Object key = entry.getKey();
			Object value = entry.getValue();
			out.println("key=" + key + "; value=" + value);
		}

		// 判断是否包含指定的 key value
		out.println(mvm.containsValue("1", "one"));
		
		
		// MultiValueMap 的 value 值是 Collection 类型。实际上是  arrayList
		//测试类型
		out.println(mvm.get("1").getClass());
		ArrayList<String> list = (ArrayList<String>) mvm.getCollection("1");
		
		for (String s : list)
			out.println(s);

	}

	/**
	 * 遍历集合的代码
	 */
	private void printCollections() {
		// For a set or list
		ArrayList list = new ArrayList();
		for (Iterator it = list.iterator(); it.hasNext();) {
			Object element = it.next();
		}

		Map map = new HashMap();
		// For keys of a map
		for (Iterator it = map.keySet().iterator(); it.hasNext();) {
			Object key = it.next();
		}

		// For values of a map
		for (Iterator it = map.values().iterator(); it.hasNext();) {
			Object value = it.next();
		}

		// For both the keys and values of a map
		for (Iterator it = map.entrySet().iterator(); it.hasNext();) {
			Map.Entry entry = (Map.Entry) it.next();
			Object key = entry.getKey();
			Object value = entry.getValue();
		}
	}

	public static void main(String[] args) {

		CollectionsExamples c = new CollectionsExamples();
		c.mapExamples();
		
	}

}
